package com.lclz.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.lclz.model.News;
import com.lclz.model.NewsDetail;
import com.lclz.model.OSChina;
import com.lclz.model.OSChinaNewsDetail;
import com.lclz.util.JaxbUtil;
import com.lclz.util.JerseyClientUtil;
import com.lclz.util.RESTFulException;

@Service
public class NewsService {

	public List<News> getNewsList(String show){
		List<News> list = Lists.newArrayList();
		StringBuffer sb = new StringBuffer("");
		if(StringUtils.isNotBlank(show)){
			sb.append("show=").append(show);
		}
		try {
			String response =JerseyClientUtil.getXml("http://www.oschina.net", "/action/api/news_list?"+sb.toString(),"");
			OSChina tmp = JaxbUtil.xmlStr2Obj(OSChina.class, response);
			list =tmp.getNewslist().getNews();
		} catch (RESTFulException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
	public NewsDetail getNewsDetail(int id){
		try {
			String response =JerseyClientUtil.getXml("http://www.oschina.net", "/action/api/news_detail?id="+id, "");
			OSChinaNewsDetail tmp = JaxbUtil.xmlStr2Obj(OSChinaNewsDetail.class, response);
			return tmp.getNews();
		} catch (RESTFulException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
}
